// 引入HTTP模块
const http = require('http');
// 引入express模块
const express = require('express');
const compression = require('compression');

const app = express();
const server = http.createServer(app);

app.use(compression());

app.use('/', express.static(__dirname + '/public')); // 指定静态HTML文件的位置
app.use('/', express.static(__dirname + '/dist')); // 指定静态HTML文件的位置

if (process.env.NODE_ENV !== 'production') {
  const webpack = require('webpack');
  const config = require('./webpack.dev.js');
  const webpackDevMiddleware = require('webpack-dev-middleware');
  const webpackHotMiddleware = require('webpack-hot-middleware');

  const compiler = webpack(config);
  app.use(webpackDevMiddleware(compiler, {
    stats: {
      colors: true,
      chunks: false
    },
    noInfo: true,
  }));

  app.use(webpackHotMiddleware(compiler));
}

// 监听3000端口
server.listen(process.env.PORT || 3000, (err) => {
  if (!err) console.log('server is running');
});
